/*
 * @Author: 你的名字
 * @Date: 2022-10-25 23:45:14
 * @FilePath: /demo-gather/src/views/GaoDeAmap/mixins/map.js
 * @Description: 
 */
import AMapLoader from "@amap/amap-jsapi-loader";

export default {
  data() {
    return {
      map: null,
      placeSearch: null,
      //接收Search组件传输input输入框的值
      searchPlaceInput: "",
      district: null,
      polygons: [],
      map_opt: [],
      mapOpt:[],
      polyEditor: null, // 多边形编辑器
      mouseTool: null,
      polygon: null, // 页面上画的多边形
      circle: null, // 页面上画的圆
      regionPath: [], // 行政区数据
      xingzhengPolygon: [], // 选择的行政区划画的边界
      type: "circle", // 多边形 polygon、圆形 circle
    }
  },
  mounted() {

    this.$EventBus.$on("aMsg", (msg) => {
      this.addNewAdministrativeDivision(msg);
    });
    this.$EventBus.$on("drawCircleNew", () => {
      this.drawCircleNew();
    });
    this.$EventBus.$on("drawPolygonNew", () => {
      this.drawPolygonNew();
    });
    this.$EventBus.$on("clearEliminate", () => {
      this.clearEliminate();
    });
    setTimeout(() => {
      this.initEchartsMap("echartsMap");
    }, 1000);
  },
  methods: {
        //地图刷新
        initEchartsMap(id) {
          AMapLoader.load({
            key: "ac951d44ce259e9a6984c04f69d16fa2",
            version: "2.0", // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
            plugins: [
              "AMap.Heatmap",
              "AMap.Autocomplete",
              "AMap.MouseTool",
              "AMap.PlaceSearch",
              "AMap.PolyEditor",
              "AMap.CircleEditor",
              "AMap.DistrictSearch",
            ], // 需要使用的的插件列表，如比例尺'AMap.Scale'等
          })
            .then((AMap) => {
              // var markList = [];
              this.map = new AMap.Map(id, {
                zoom: 10,
                center: [113.747744, 34.749966],
                pitch: 0,
                viewMode: "2D",
                mapStyle: "amap://styles/darkblue",
              });
              this.map.on("click", (e) => {
                this.showInfoClick(e);
              });
              this.mouseTool = new AMap.MouseTool(this.map); // 通过该插件，可进行鼠标画标记点、线、多边形、矩形、圆、距离量测、面积量测、拉框放大、拉框缩小等功能
              this.mapOpt.push({ city: this.city, color: "#409EFF" });
    
              this.getSearch(this.mapOpt);
            })
            .catch(() => {});
          // 创建地图
        },
    /**
     * 重新显示选中的区域范围
     *
     * @param   {[type]}  address  [address description]
     *
     * @return  {[type]}           [return description]
     */
    addNewAdministrativeDivision(address) {
      this.mapOpt = [];
      this.mapOpt.push({ city: address, color: "#409EFF" });
      this.getSearch(this.mapOpt);
    },
    getSearch(e) {
      this.searchBounds(e);
    },
    // 行政区区域划分
    searchBounds(newArr) {
      // 每次搜索之后清空上一次搜索的数据
      if (this.polygons != null) {
        this.map.remove(this.polygons); //清除上次结果
        this.polygons = [];
      }
      //加载行政区划插件
      if (!this.district) {
        //实例化DistrictSearch
        var opts = {
          subdistrict: 0, //获取边界不需要返回下级行政区
          extensions: "all", //返回行政区边界坐标组等具体信息
          level: "district" //查询行政级别为 市
        };
        this.map.plugin(["AMap.DistrictSearch"], () => {
          /* eslint-disable */
          this.district = new AMap.DistrictSearch(opts);
        });
      }
      // 循环数组查询
      for (let i = 0; i < newArr.length; i++) {
        // 行政区查询
        this.district.search(newArr[i].city, (status, result) => {
          this.handlePolygon(result, newArr[i].color);
        });
      }
    },
    // 画图方法
    handlePolygon(result, color) {
      let bounds = result.districtList[0].boundaries;
      if (bounds) {
        for (let i = 0, l = bounds.length; i < l; i++) {
          //生成行政区划polygon
          let polygon = new AMap.Polygon({
            map: this.map, // 指定地图对象
            strokeWeight: 1, // 轮廓线宽度
            path: bounds[i], //轮廓线的节点坐标数组
            fillOpacity: 0.4, //透明度
            fillColor: color, //填充颜色
            strokeColor: "#256edc" //线条颜色
          });
          this.polygons.push(polygon);
        }
        // 地图自适应
        this.map.setFitView();
      }
    },
    drawCircleNew(){
      this.map.setDefaultCursor("crosshair");
      this.clearEliminate(); // 清除覆盖物
    },
    drawPolygonNew(){},
   // 清除覆盖物 如果type为false 才清除手动输入的值
   clearEliminate(type) {
    // 清除覆盖物
    if (this.polyEditor) {
      this.polyEditor.close();
      this.mouseTool.close(true);
    }
    if (this.polygon) {
      this.map.remove(this.polygon);
    }
    if (this.circle) {
      this.map.remove(this.circle);
    }
    if (this.circleEditor) {
      this.circleEditor.close();
    }
    if (this.xingzhengPolygon) {
      this.map.remove(this.xingzhengPolygon);
    }
    if (!type) {
      this.circleRadiusNew = 0; // 手动画圆半径
      this.centerLngLatNew = {
        lng: "",
        lat: "",
      }; // 手动画圆经纬度
    }

    this.path = []; // 手动画多边形数组
    this.regionPath = [];
  },
  }
}